{ *
  * Copyright (C) 2012-2013 ozok <ozok26@gmail.com>
  *
  * This file is part of TAudioConverter.
  *
  * TAudioConverter is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 2 of the License, or
  * (at your option) any later version.
  *
  * TAudioConverter is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with TAudioConverter.  If not, see <http://www.gnu.org/licenses/>.
  *
  * }

unit UnitSettings;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, sSkinProvider, Vcl.StdCtrls, sComboBox,
  sCheckBox, sEdit, sButton, IniFiles, Registry, ShellAPI, JvSpin, Vcl.ExtCtrls,
  sPanel;

type
  TSettingsForm = class(TForm)
    sSkinProvider1: TsSkinProvider;
    ResetTmpBtn: TsButton;
    SelectTmpBtn: TsButton;
    TempEdit: TsEdit;
    CheckUpdateBtn: TsCheckBox;
    TagsBtn: TsCheckBox;
    CPUBtn: TsCheckBox;
    ProcessCountList: TsComboBox;
    SkinList: TsComboBox;
    PostEncode2List: TsComboBox;
    ResetProcessBtn: TsButton;
    FolderStructBtn: TsCheckBox;
    ShellRegisterBtn: TsButton;
    ShellUnregisterBtn: TsButton;
    SeeLogBtn: TsButton;
    AACExtList: TsComboBox;
    ArtworkBtn: TsCheckBox;
    SkinBtn: TsCheckBox;
    OpenDialog1: TOpenDialog;
    Artwork2Btn: TsCheckBox;
    LyricBTn: TsCheckBox;
    FolderSuffixBtn: TsCheckBox;
    ArtworkSuffixBtn: TsCheckBox;
    ArtPanel: TsPanel;
    EncodingPanel: TsPanel;
    GeneralPanel: TsPanel;
    SettingsList: TsComboBox;
    FolderStruct2Btn: TsCheckBox;
    ArtworkList: TsComboBox;
    procedure PostEncode2ListChange(Sender: TObject);
    procedure SkinListChange(Sender: TObject);
    procedure ResetProcessBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SelectTmpBtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ShellRegisterBtnClick(Sender: TObject);
    procedure ShellUnregisterBtnClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure SeeLogBtnClick(Sender: TObject);
    procedure SkinBtnClick(Sender: TObject);
    procedure ResetTmpBtnClick(Sender: TObject);
    procedure SettingsListChange(Sender: TObject);
    procedure FolderStructBtnClick(Sender: TObject);
    procedure ArtworkBtnClick(Sender: TObject);
    procedure ArtworkListChange(Sender: TObject);
  private
    { Private declarations }
    // save/load options
    procedure LoadOptions();
    procedure SaveOptions();

    // shell extension
    procedure ShellExtensionRegister();
    procedure ShellExtensionUnRegister();

  public
    { Public declarations }
  end;

var
  SettingsForm: TSettingsForm;

implementation

{$R *.dfm}

uses UnitMain;

{ TForm1 }

procedure TSettingsForm.ArtworkBtnClick(Sender: TObject);
begin

  ArtworkList.Enabled := ArtworkBtn.Checked;

  if ArtworkBtn.Checked then
  begin
    ArtworkList.OnChange(self);
  end
  else
  begin
    Artwork2Btn.Enabled := False;
    ArtworkSuffixBtn.Enabled := False;
  end;

end;

procedure TSettingsForm.ArtworkListChange(Sender: TObject);
begin

  if ArtworkList.ItemIndex = 0 then
  begin
    Artwork2Btn.Enabled := True;
    ArtworkSuffixBtn.Enabled := True;
  end
  else
  begin
    Artwork2Btn.Enabled := False;
    ArtworkSuffixBtn.Enabled := False;
  end;

end;

procedure TSettingsForm.FolderStructBtnClick(Sender: TObject);
begin

  FolderStruct2Btn.Enabled := FolderStructBtn.Checked;

end;

procedure TSettingsForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  SaveOptions;
  MainForm.Enabled := True;
  MainForm.BringToFront;

end;

procedure TSettingsForm.FormCreate(Sender: TObject);
var
  i: integer;
begin
  // skins
  for I := 0 to MainForm.sSkinManager1.InternalSkins.Count - 1 do
  begin
    SkinList.Items.Add(MainForm.sSkinManager1.InternalSkins[i].Name);
  end;
  SkinList.ItemIndex := 1;

  LoadOptions;
end;

procedure TSettingsForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

  if Key = VK_ESCAPE then
  begin
    self.Close;
  end;

end;

procedure TSettingsForm.LoadOptions;
var
  SettingsFile: TIniFile;
begin

  SettingsFile := TIniFile.Create(MainForm.AppData + 'settings.ini');
  try

    with SettingsFile do
    begin
      TempEdit.Text := ReadString('Settings', 'Temp',
        MainForm.SystemInfo.Folders.Temp + '\TAudioConverter\');

      CheckUpdateBtn.Checked := ReadBool('Settings', 'Update', True);

      PostEncode2List.ItemIndex := ReadInteger('Settings', 'Post', 0);

      ProcessCountList.ItemIndex := ReadInteger('Settings', 'Process',
        MainForm.DecideNumOfProcesses - 1);
      TagsBtn.Checked := ReadBool('Settings', 'Tags', True);
      CPUBtn.Checked := ReadBool('Settings', 'CPU', False);
      SkinList.ItemIndex := ReadInteger('Settings', 'Skin', 3);
      SkinBtn.Checked := ReadBool('Settings', 'Skin2', False);
      FolderStructBtn.Checked := ReadBool('Settings', 'folder', True);
      FolderStruct2Btn.Checked := ReadBool('Settings', 'folder2', True);
      AACExtList.ItemIndex := ReadInteger('Settings', 'AAC', 0);
      ArtworkBtn.Checked := ReadBool('Settings', 'artwork', False);
      Artwork2Btn.Checked := ReadBool('Settings', 'artwork2', False);
      ArtworkSuffixBtn.Checked := ReadBool('Settings', 'ArtWorkSuf', False);
      FolderSuffixBtn.Checked := ReadBool('Settings', 'FolderSuf', True);
      LyricBTn.Checked := ReadBool('settings', 'Lyric', False);
      ArtworkList.ItemIndex := ReadInteger('settings', 'artworklist', 0);

      if ReadInteger('Settings', 'Settings', 0) = 3 then
      begin
        SettingsList.ItemIndex := 0;
      end
      else
      begin
        SettingsList.ItemIndex := ReadInteger('Settings', 'Settings', 0);
      end;
    end;

  finally
    SettingsFile.Free;
    SettingsList.OnChange(self);
    FolderStructBtn.OnClick(self);
    ArtworkBtn.OnClick(self);
  end;

end;

procedure TSettingsForm.PostEncode2ListChange(Sender: TObject);
begin
  MainForm.PostEncodeList.ItemIndex := PostEncode2List.ItemIndex;
end;

procedure TSettingsForm.ResetProcessBtnClick(Sender: TObject);
begin
  ProcessCountList.ItemIndex := MainForm.DecideNumOfProcesses - 1;
end;

procedure TSettingsForm.ResetTmpBtnClick(Sender: TObject);
begin

  TempEdit.Text := MainForm.GetTempDirectory;

end;

procedure TSettingsForm.SaveOptions;
var
  SettingsFile: TIniFile;
begin

  SettingsFile := TIniFile.Create(MainForm.AppData + 'settings.ini');
  try

    with SettingsFile do
    begin
      WriteString('Settings', 'Temp', TempEdit.Text);

      WriteBool('Settings', 'Update', CheckUpdateBtn.Checked);

      WriteInteger('Settings', 'Post', PostEncode2List.ItemIndex);

      WriteInteger('Settings', 'Process', ProcessCountList.ItemIndex);
      WriteBool('Settings', 'Tags', TagsBtn.Checked);
      WriteBool('Settings', 'CPU', CPUBtn.Checked);
      WriteInteger('Settings', 'Skin', SkinList.ItemIndex);
      WriteBool('Settings', 'Skin2', SkinBtn.Checked);
      WriteBool('Settings', 'folder', FolderStructBtn.Checked);
      WriteBool('Settings', 'folder2', FolderStruct2Btn.Checked);
      WriteInteger('Settings', 'AAC', AACExtList.ItemIndex);
      WriteBool('Settings', 'artwork', ArtworkBtn.Checked);
      WriteBool('Settings', 'artwork2', Artwork2Btn.Checked);
      WriteBool('Settings', 'ArtWorkSuf', ArtworkSuffixBtn.Checked);
      WriteBool('Settings', 'FolderSuf', FolderSuffixBtn.Checked);
      WriteBool('settings', 'Lyric', LyricBTn.Checked);
      WriteInteger('settings', 'artworklist', ArtworkList.ItemIndex);

      WriteInteger('Settings', 'Settings', SettingsList.ItemIndex);
    end;

  finally
    SettingsFile.Free;
    SkinBtn.OnClick(self);
  end;

end;

procedure TSettingsForm.SeeLogBtnClick(Sender: TObject);
begin
  ShellExecute(Application.Handle, 'open', PChar(MainForm.AppData), nil, nil,
    SW_SHOWNORMAL);
end;

procedure TSettingsForm.SelectTmpBtnClick(Sender: TObject);
begin

  if MainForm.OpenFolderDialog.Execute then
  begin
    if ExtractFileDir(Application.ExeName) <> MainForm.OpenFolderDialog.Directory
    then
    begin
      TempEdit.Text := MainForm.OpenFolderDialog.Directory;
    end
    else
    begin
      Application.MessageBox
        ('Please select a folder other than TAC''s program folder.', 'Warning',
        MB_ICONWARNING);
    end;
  end;

end;

procedure TSettingsForm.SettingsListChange(Sender: TObject);
begin

  case SettingsList.ItemIndex of
    0:
      begin
        GeneralPanel.Visible := True;
        EncodingPanel.Visible := False;
        ArtPanel.Visible := False;
      end;
    1:
      begin
        GeneralPanel.Visible := False;
        EncodingPanel.Visible := True;
        ArtPanel.Visible := False;
      end;
    2:
      begin
        GeneralPanel.Visible := False;
        EncodingPanel.Visible := False;
        ArtPanel.Visible := True;
      end;
    3:
      begin
        GeneralPanel.Visible := False;
        EncodingPanel.Visible := False;
        ArtPanel.Visible := False;
      end;
  end;

end;

procedure TSettingsForm.ShellExtensionRegister();
var
  sTypeName: string;
begin
  // taken from http://www.delphidabbler.com/tips/15
  with TRegistry.Create do
  begin

    try
      RootKey := HKEY_CLASSES_ROOT;

      if OpenKey('*', True) then
      begin
        try

          if OpenKey('shell', True) then
          begin

            if OpenKey('TAC', True) then
            begin
              WriteString('', 'Encode with TAudioConverter');
              WriteString('Icon', '"' + Application.ExeName + '"');

              if OpenKey('command', True) then
              begin
                WriteString('', '"' + Application.ExeName + '" "%1"');
                Application.MessageBox('Register successful.', 'Info',
                  MB_ICONINFORMATION);
              end
              else
              begin
                Application.MessageBox('Cannot register. Error:3', 'Error',
                  MB_ICONERROR);
              end;

            end
            else
            begin
              Application.MessageBox('Cannot register. Error:2', 'Error',
                MB_ICONERROR);
            end;

          end
          else
          begin
            Application.MessageBox('Cannot register. Error:1', 'Error',
              MB_ICONERROR);
          end;

        finally
          CloseKey;
        end;

      end
      else
      begin
        Application.MessageBox('Cannot register. Error:0', 'Error',
          MB_ICONERROR);
      end;

    finally
      Free;
    end;

  end;

end;

procedure TSettingsForm.ShellExtensionUnRegister;
var
  sTypeName: string;
begin
  // taken from http://www.delphidabbler.com/tips/15
  with TRegistry.Create do
  begin

    try
      RootKey := HKEY_CLASSES_ROOT;

      if OpenKey('*', True) then
      begin
        try

          if OpenKey('shell', True) then
          begin

            if DeleteKey('TAC') then
            begin
              Application.MessageBox('Unregister succesfull.', 'Unregister',
                MB_ICONINFORMATION);
            end
            else
            begin
              Application.MessageBox('Cannot unregister. Error:2', 'Error',
                MB_ICONERROR);
            end;

          end
          else
          begin
            Application.MessageBox('Cannot unregister. Error:1', 'Error',
              MB_ICONERROR);
          end;

        finally
          CloseKey;
        end;

      end
      else
      begin
        Application.MessageBox('Cannot unregister. Error:0', 'Error',
          MB_ICONERROR);
      end;

    finally
      Free;
    end;

  end;

end;

procedure TSettingsForm.ShellRegisterBtnClick(Sender: TObject);
begin

  ShellExtensionRegister();

end;

procedure TSettingsForm.ShellUnregisterBtnClick(Sender: TObject);
begin

  ShellExtensionUnRegister;

end;

procedure TSettingsForm.SkinBtnClick(Sender: TObject);
begin

  SkinList.Enabled := not SkinBtn.Checked;
  MainForm.sSkinManager1.Active := not SkinBtn.Checked;
  if MainForm.sSkinManager1.Active then
  begin
    MainForm.sSkinManager1.RepaintForms(False);
    MainForm.ProgressPanel.Repaint;
    self.Repaint;
    MainForm.Repaint;
  end;

end;

procedure TSettingsForm.SkinListChange(Sender: TObject);
var
  i: integer;
begin

  MainForm.sSkinManager1.SkinName := MainForm.sSkinManager1.InternalSkins
    [SkinList.ItemIndex].Name;
  SendMessage(MainForm.MPCQualityBar.Handle, CM_RecreateWnd, 0, 0);
  for I := 0 to MainForm.ComponentCount - 1 do
  begin
    if MainForm.Components[i] is TJvSpinEdit then
    begin
      (MainForm.Components[i] as TJvSpinEdit).Color :=
        MainForm.DirectoryEdit.Color;
      (MainForm.Components[i] as TJvSpinEdit).Font :=
        MainForm.DirectoryEdit.Font;
    end;
  end;

end;

end.
